'use strict';
/** 聊天系统 */
let CHATLIST = [];

module.exports.run = function(d,s){
    const bs = require("../module/base"), gm = require("../config/" + s.U.gamename), bag = require("./bag");
    switch(d[1]){
        case 1:{ //获取最近聊天记录
            d[2] = CHATLIST;
            bs.send(s, d);
            break;
        }case 2:{ //发送聊天
            if(!bs.checkParam(["txt","id"], d)) return;
            //道具错误
            if(!(d[2].id == 1017 || d[2].id == 1018)){
                return bs.sendE(s, 4);
            }
            //道具数量
            if(!s.U.bag[d[2].id]){
                return bs.sendE(s, 4);
            }
            if(d[2].txt.length < gm.chatlen){
                let cv = [s.U.uid, s.U.nk, d[2].txt];
                CHATLIST.push(cv);
                if(CHATLIST.length > gm.chatmax) CHATLIST.shift();
                bag.add([[d[2].id, 1]], s, null, 4);
                bs.sendUser(0, cv);
            }
            break;
        }
    }
}